package com.kk.permission.huawei.actionutil;

import android.accessibilityservice.AccessibilityService;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kk.permission.IPermissionGuideStrategy;
import com.kk.permission.accessibilityutils.NodeUtil;
import com.kk.permission.huawei.HuaweiTool;

import java.util.HashMap;

public class HuaweiSystemDialingUtil {
    public static String TAG = "systemDialing";
    private final String STEP1 = "step1";
    private final String STEP2 = "step2";
    private final String STEP3 = "step3";
    private final String STEP4 = "step4";
    private final String STEP5 = "step5";
    private String mAppName = HuaweiTool.getAppName();
    private HashMap<String, String> mStepMap = new HashMap();
    private boolean result = false;

    public boolean systemDialing(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (this.mStepMap.containsKey("step4")) {
            Log.e(TAG, "step:step4");
            if (this.mStepMap.containsKey("step5")) {
                Log.e(TAG, "step:back");
                NodeUtil.back(accessibilityService);
                Log.e(TAG, "step:back=true");
            } else {
                Log.e(TAG, "step:更换0000");
                if (NodeUtil.pageContains(accessibilityNodeInfo, "更换")) {
                    Log.e(TAG, "step:更换11111");
                    NodeUtil.clickByText(accessibilityNodeInfo, "更换");
                    this.mStepMap.put("step5", "");
                    Log.e(TAG, "step:step5");
                }
                if (IPermissionGuideStrategy.isDefaultPhoneApp()) {
                    Log.e(TAG, "step:isdefault");
                    NodeUtil.back(accessibilityService);
                }
            }
            return true;
        }
        if (this.mStepMap.containsKey("step5")) {
            NodeUtil.back(accessibilityService);
        } else if (!this.mStepMap.containsKey("step1") && NodeUtil.pageContains(accessibilityNodeInfo, "高级") && NodeUtil.pageContains(accessibilityNodeInfo, "应用管理")) {
            NodeUtil.clickByText(accessibilityNodeInfo, "高级");
            this.mStepMap.put("step1", "");
            Log.e(TAG, "step:step1");
        }
        if (this.mStepMap.containsKey("step5")) {
            NodeUtil.back(accessibilityService);
        } else if (!this.mStepMap.containsKey("step1") && NodeUtil.pageContains(accessibilityNodeInfo, "设置")) {
            NodeUtil.clickByText(accessibilityNodeInfo, "设置");
            this.mStepMap.put("step1", "");
            Log.e(TAG, "step:step1");
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, "默认应用设置") && NodeUtil.pageContains(accessibilityNodeInfo, "应用权限")) {
            NodeUtil.clickByText(accessibilityNodeInfo, "默认应用设置");
            this.mStepMap.put("step2", "");
            Log.e(TAG, "step:step2");
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, "拨号") && NodeUtil.pageContains(accessibilityNodeInfo, "桌面")) {
            NodeUtil.clickByText(accessibilityNodeInfo, "拨号");
            this.mStepMap.put("step3", "");
            Log.e(TAG, "step:step3");
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, this.mAppName) && NodeUtil.pageContains(accessibilityNodeInfo, "拨号")) {
            NodeUtil.clickByText(accessibilityNodeInfo, this.mAppName);
            this.mStepMap.put("step4", "");
            Log.e(TAG, "step:step4");
        }
        return false;
    }

    public boolean systemDialingBNDAL00SDK26(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (!this.mStepMap.containsKey("step3")) {
            if (this.mStepMap.containsKey("step4")) {
                NodeUtil.back(accessibilityService);
            }
            if (NodeUtil.pageContains(accessibilityNodeInfo, "默认应用")) {
                this.mStepMap.put("step1", "");
                NodeUtil.clickByText(accessibilityNodeInfo, "默认应用");
            }
            if (NodeUtil.pageContains(accessibilityNodeInfo, "拨号") && !NodeUtil.pageContains(accessibilityNodeInfo, "应用设置")) {
                this.mStepMap.put("step2", "");
                NodeUtil.clickByText(accessibilityNodeInfo, "拨号");
            }
            if (NodeUtil.pageContains(accessibilityNodeInfo, this.mAppName)) {
                this.mStepMap.put("step3", "");
                NodeUtil.clickByText(accessibilityNodeInfo, this.mAppName);
                HuaweiTool.actionSystemDialingDone();
            }
            return false;
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, "更换")) {
            this.mStepMap.put("step4", "");
            NodeUtil.clickByText(accessibilityNodeInfo, "更换");
            HuaweiTool.actionSystemDialingDone();
            NodeUtil.back(accessibilityService);
            return true;
        } else {
            NodeUtil.back(accessibilityService);
            return true;
        }
    }
}
